package cn.wolfcode.car.business.service.impl;

import cn.wolfcode.car.business.domain.Department;
import cn.wolfcode.car.business.domain.Employee;
import cn.wolfcode.car.business.mapper.DepartmentMapper;
import cn.wolfcode.car.business.mapper.EmployeeMapper;
import cn.wolfcode.car.business.query.EmployeeQuery;
import cn.wolfcode.car.business.service.IEmployeeService;
import cn.wolfcode.car.common.base.page.TablePage;
import cn.wolfcode.car.common.base.page.TablePageInfo;
import cn.wolfcode.car.common.util.Convert;
import cn.wolfcode.car.common.util.StringUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class EmployeeServiceImpl extends ServiceImpl<EmployeeMapper, Employee> implements IEmployeeService {

    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    public TablePage<Employee> query(EmployeeQuery qo) {
        Page<Employee> page = new Page<>(qo.getPageNum(), qo.getPageSize());
        QueryWrapper<Employee> wrapper = Wrappers.<Employee>query();
        wrapper.like(StringUtils.isNotEmpty(qo.getKeyword()), "name", qo.getKeyword())
                .or().like(StringUtils.isNotEmpty(qo.getKeyword()), "email", qo.getKeyword());
        wrapper.eq(StringUtils.isNotNull(qo.getAdmin()), "admin", qo.getAdmin());
        wrapper.eq(StringUtils.isNotNull(qo.getStatus()), "status", qo.getStatus());
        wrapper.eq(StringUtils.isNotNull(qo.getDeptId()), "dept_id", qo.getDeptId());
        wrapper.ge(StringUtils.isNotNull(qo.getBeginTime()), "hiredate", qo.getBeginTime());
        wrapper.le(StringUtils.isNotNull(qo.getEndTime()), "hiredate", qo.getEndTime());
        employeeMapper.selectPage(page, wrapper);
        List<Employee> list = page.getRecords();
        for (Employee employee : list) {
            Long deptId = employee.getDeptId();
            Department department = departmentMapper.selectById(deptId);
            employee.setDept(department);
        }
        page.setRecords(list);
        return new TablePage<Employee>(page);
    }


    @Override
    public void add(Employee employee) {
        // 设置默认为未冻结
        employee.setStatus(Employee.STATUS_NO);
        employeeMapper.insert(employee);
    }

    @Override
    public Employee get(Long id) {
        Employee employee = employeeMapper.selectById(id);
        Long deptId = employee.getDeptId();
        Department department = departmentMapper.selectById(deptId);
        employee.setDept(department);
        return employee;
    }


    @Override
    public void update(Employee employee) {
        Employee oldEmp = employeeMapper.selectById(employee.getId());
        employee.setHiredate(oldEmp.getHiredate());
        employeeMapper.updateById(employee);
    }

    @Override
    public void deleteBatch(String ids) {
        Long[] dictIds = Convert.toLongArray(ids);
        for (Long dictId : dictIds) {
            employeeMapper.deleteById(dictId);
        }
    }

    @Override
    public List<Department> listDept() {
        return departmentMapper.selectList(Wrappers.emptyWrapper());
    }

    @Override
    public Department getDepartmentDetail(Long id) {
        Employee employee = employeeMapper.selectById(id);
        return departmentMapper.selectById(employee.getDeptId());
    }
}
